﻿using System.IO.IsolatedStorage;
using System.Windows;
using GalaSoft.MvvmLight;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace SAFPodcast.WP8.Fwk
{
    public class LockScreenManager : ObservableObject, ILockScreenManager
    {
        #region Singleton
        static LockScreenManager() { }
        private static readonly ILockScreenManager instance = new LockScreenManager();
        public static ILockScreenManager Instance { get { return instance; } }
        #endregion
        #region Constantes
        private const string promptedKey = "UserPromptedToAllowRunningUnderLockScreen";
        private const string runningEnabledKey = "RunningUnderLockScreenEnabled";
        #endregion
        #region Field
        private bool runningUnderLockScreenEnabled;
        #endregion
        #region Constructor
        private LockScreenManager()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            bool prompted;
            if (settings.TryGetValue(promptedKey, out prompted))
            {
                UserPrompted = prompted;
            }

            bool enabledValue;
            if (settings.TryGetValue(runningEnabledKey, out enabledValue))
            {
                RunningUnderLockScreenEnabled = enabledValue;
            }

            var frame = GetRootFrame(Application.Current);
            frame.Obscured += (o, args) => RunningUnderLockScreen = args.IsLocked;
            frame.Unobscured += (o, args) => RunningUnderLockScreen = false;
        }

        private PhoneApplicationFrame GetRootFrame(Application application)
        {
            var prop = application.GetType().GetProperty("RootFrame");
            return (PhoneApplicationFrame)prop.GetValue(application, null);
        }
        #endregion
        #region Properties
        public bool RunningUnderLockScreen { get; set; }
        private bool userPrompted;
        public bool UserPrompted
        {
            get
            {
                return userPrompted;
            }
            set
            {
                userPrompted = value;
                SaveSetting(promptedKey, userPrompted);
            }
        }
        public bool RunningUnderLockScreenEnabled
        {
            get
            {
                return runningUnderLockScreenEnabled;
            }
            set
            {
                Set(() => RunningUnderLockScreenEnabled, ref runningUnderLockScreenEnabled, value);
                if (runningUnderLockScreenEnabled)
                {
                    PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
                }

                SaveSetting(runningEnabledKey, runningUnderLockScreenEnabled);
            }
        }
        #endregion
        #region Private methods
        void SaveSetting(string key, object value)
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            settings[key] = value;
            settings.Save();
        }
        #endregion
    }
}
